package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.publication.service.PublicationService;
import org.ediba.siri.core.user.domain.User;
import org.ediba.siri.core.user.domain.UserGroup;
import org.ediba.siri.core.user.domain.UserRole;
import org.ediba.siri.core.user.service.UserService;
import org.json.JSONException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ResponseBody;

public class UserController extends Controller {

	private PublicationService publicationService;
	public void setPublicationService(PublicationService publicationService) {this.publicationService = publicationService;}
	
	private UserService userService;
	public void setUserService(UserService userService) {this.userService = userService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse respons, Command command){
		String name = null;
		String password = "1234";
		try {name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		User user = this.userService.create(name, password);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", user);
		_result.put("success", true);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, UserCommand userCommand){
		
		Integer id = userCommand.getId();
		String name = userCommand.getName();
		String password = userCommand.getPassword();
		String email = userCommand.getEmail();
		String firstName = userCommand.getFirstName();
		String lastName = userCommand.getLastName();

		if (userCommand.getData() != null)
			try {
				id = userCommand.getData().getInt("id");
				name = userCommand.getData().getString("name");
				//password = userCommand.getData().getString("password");
				email = userCommand.getData().getString("email");
				firstName = userCommand.getData().getString("firstName");
				lastName = userCommand.getData().getString("lastName");
			} catch (JSONException e) {e.printStackTrace();}
		
		User user = this.userService.retrieve(id);
		if (name!=null)
			user.setName(name);
		if (firstName!=null)
			user.setFirstName(firstName);
		if (lastName!=null)
			user.setLastName(lastName);
		if (email!=null)
			user.setEmail(email);
		if ((password!=null)&&(!password.equalsIgnoreCase("")))
			user.setPassword(password);
		user = this.userService.update(user);

		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, UserCommand command){
		boolean current  = ServletRequestUtils.getBooleanParameter(request, "current",false);
		
		List<User> users = null;
		List<User> usersAll = null;
		if (current){
			users = new ArrayList<User>(1);
			users.add(this.userService.retrieveCurrent());
			usersAll = users;
		}
		else if (command.getIdsUserGroup() != null) {
			users = new ArrayList<User>(this.userService.groupRetrieve(command.getIdsUserGroup()[0]).getUsers());
			usersAll = users;
		}
		else if (command.getIdsPublication() != null) {
			users = new ArrayList<User>(this.publicationService.retrieve(command.getIdsPublication()[0]).getUsers());
			usersAll = users;
		}
		else if (command.getIdsPublication1() != null) {
			users = new ArrayList<User>(this.publicationService.retrieve(command.getIdsPublication1()[0]).getUsers_1());
			usersAll = users;
		}
		else{
			usersAll = this.userService.retrieve();
			if (command.getSort() != null)
				users = this.userService.retrieve(command.getSort(),command.getDir(),command.getStart(),command.getLimit());
			else 
				users = usersAll;
		}
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", users);
		_result.put("total", usersAll.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}

	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, UserCommand userCommand){
		Integer id = null;
		try {id = userCommand.getData().getInt("id");}
		catch (JSONException e) {e.printStackTrace();}
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		boolean _success = false;
		try {_success = this.userService.delete(id);}
		catch (IllegalArgumentException e) {_result.put("message", e.getMessage());}
		
		_result.put("success", _success);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> attachUserRoleHandler(HttpServletRequest request, HttpServletResponse response, UserCommand command){

		User user = this.userService.retrieve(command.getId());
		UserRole userRole = this.userService.roleRetrieve(command.getIdsUserRole()[0]);

		user.getUserroles().add(userRole);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deattachUserRoleHandler(HttpServletRequest request, HttpServletResponse response, UserCommand command){
		
		User user = this.userService.retrieve(command.getId());
		UserRole userRole = this.userService.roleRetrieve(command.getIdsUserRole()[0]);
		
		user.getUserroles().remove(userRole);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> attachUserGroupHandler(HttpServletRequest request, HttpServletResponse response, UserCommand command){
		
		User user = this.userService.retrieve(command.getId());
		UserGroup userGroup = this.userService.groupRetrieve(command.getIdsUserGroup()[0]);
		
		user.getUsergroups().add(userGroup);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deattachUserGroupHandler(HttpServletRequest request, HttpServletResponse response, UserCommand command){
		
		User user = this.userService.retrieve(command.getId());
		UserGroup userGroup = this.userService.groupRetrieve(command.getIdsUserGroup()[0]);
		
		user.getUsergroups().remove(userGroup);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
}
